<!--
 * @Author: zhangpeng
 * @Date: 2024-11-14 16:58:07
 * @FilePath: \scientific_research_manager_web\src\views\project\mine\submitIntermediateInspection.vue
 * @LastEditTime: 2025-01-08 15:57:42
 * @Description: 提交中期检查
-->
<template>
  <div class="submit-intermediate" v-loading="loading || detailLoading">
    <MyProjectHeader :project-id="projectId" :project-name="projectName" />

    <div class="submit-intermediate-content">
      <el-form :model="formDate" label-width="auto" class="mt-[20px]">
        <el-form-item label="中检文件" prop="fileList">
          <FileUpload :is-show-tip="false" drag width="393px" @update:modelValue="update"
            :model-value="formDate.fileList" />
        </el-form-item>

      </el-form>

      <div class="h-[72px] bg-white"></div>
      <div class="examine-bar">
        <el-button @click="proxy.$tab.closePage()" v-if="route.query.type !== 'edit'">取消</el-button>
        <el-button type="primary" @click="submit">{{ route.query.type === 'edit' ? '保存' : '提交中检'}}</el-button>
      </div>

    </div>
  </div>
</template>

<script lang="ts">
export default defineComponent({
  beforeRouteEnter(to, from, next) {
    if(to.query.type === 'edit'){
      to.meta.title = '编辑中检';
    }
    
    next()
  }
})
</script>

<script setup lang="ts" name="SubmitIntermediateInspection">
import { ref } from 'vue';
import { projectApproveSubmitApi, getApproveMaterialDetailsApi } from '@/api/project'
import { projectSubmitTypeEnum } from '@/utils/constEnum'
import MyProjectHeader from '../components/MyProject/MyProjectHeader.vue'

const { proxy } = getCurrentInstance();
const route = useRoute()
const projectId = ref(route.query.projectId as unknown as string)
const projectName = ref(route.query.projectName as unknown as string)
const fileIds = computed(() => {
  return formDate.value.fileList?.map((i) => i?.id)
})

const formDate = ref({
  fileList: [], //  中检文件
})

function update(val) {
  formDate.value.fileList = val
}

// 提交中检
const { run: projectApproveSubmit, loading } = useRequest(projectApproveSubmitApi, {
  manual: true,
  debounceWait: 500,
  onSuccess: (res) => {
    if (res?.isSuccess) {
      proxy.$modal.msgSuccess(route.query.type === 'edit' ? "保存成功" : "提交成功");
      close()
    }
  }
})

// 中检材料
const { run: getApproveMaterialDetails, loading: detailLoading } = useRequest(getApproveMaterialDetailsApi, {
  manual: true,
  onSuccess: (res) => {
    formDate.value.fileList = res?.attachmentFileInfoDTOList
  }
})



function close() {
  proxy.$tab.closePage();
}


function submit() {
  projectApproveSubmit({
    projectId: projectId.value,
    fileIds: fileIds.value,
    type: projectSubmitTypeEnum.submitIntermediate,
  })
}
onMounted(() => {
  if (route.query.type === 'edit') {
    getApproveMaterialDetails({
      projectId: projectId.value,
      materialType: 1,
    })
  }

})

</script>

<style lang="scss" scoped>
.submit-intermediate {
  --at-apply: bg-white;
  min-height: 100%;

  &-content {
    --at-apply: p-[20px];
  }
}


.examine-bar {
  border-top: 1px solid #E5E6EB;
  --at-apply: fixed bottom-0 left-0 right-0 h-[72px] flex items-center justify-center bg-white z-1999;

  :deep(.el-button) {
    height: 32px;
  }
}
</style>